package com.atguigu.controller;



import com.atguigu.entity.BaseSaleProperty;
import com.atguigu.entity.ProductSpu;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseSalePropertyService;
import com.atguigu.service.ProductSpuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author yangchaolove
 * @since 2021-11-18
 */
@RestController
@RequestMapping("/product")
public class SpuController {

    @Autowired
    private ProductSpuService spuService;
    @Autowired
    private BaseSalePropertyService salePropertyService;

    /** product-SPU
     * 根据分类id查询商品spu列表
     * @param pageNum       当前页
     * @param pageSize      每页数量
     * @param category3Id   三级分裂id
     * @return
     */
    @GetMapping("/queryProductSpuByPage/{pageNum}/{pageSize}/{category3Id}")
    public RetVal queryProductSpuByPage(
            @PathVariable Long pageNum,
            @PathVariable Long pageSize,
            @PathVariable Long category3Id){
        //创建一个Page对象
        Page<ProductSpu> productSpuPage = new Page<>(pageNum, pageSize);
        QueryWrapper<ProductSpu> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id", category3Id);
        wrapper.orderByDesc("id");
        spuService.page(productSpuPage, wrapper);
        return RetVal.ok(productSpuPage);
    }
    /**
     * 	查询所有的基本销售属性
     */
    @GetMapping("/queryAllSaleProperty")
    public RetVal queryAllSaleProperty(){
        List<BaseSaleProperty> salePropertyList = salePropertyService.list(null);
        return RetVal.ok(salePropertyList);
    }
    //保存商品SPU信息
    @PostMapping("saveProductSpu")
    public RetVal saveProductSpu(@RequestBody ProductSpu productSpu){
        spuService.saveProductSpu(productSpu);
        return RetVal.ok();
    }


}

